//
//  ResumeListCtrl.m
//  laowu
//
//  Created by 尹桥印 on 15/6/2.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "ResumeListCtrl.h"
#import "ResumeItemCell.h"
#import "HomeDataTool.h"
#import "ResumeCenterCtrl.h"
#import "TimeUtils.h"
#import "RecruitmentCtrl.h"
#import "LWLoginCtrl.h"

@interface ResumeListCtrl ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,copy) NSString* keywords;
@property(nonatomic,strong) NSString* city;
@property(nonatomic,copy) NSArray* jobArr;
@property(nonatomic,strong) PublishDateModel* date;



@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *itemArray;


@property(nonatomic,copy)NSMutableString* job;
@property(nonatomic,copy)NSString *start;
@property(nonatomic,copy)NSString *end;



@end

@implementation ResumeListCtrl

-(instancetype)initWithSearch:(NSString*)keywords city:(NSString*)city job:(NSArray*)job date:(PublishDateModel*)date{
    self = [super init];
    if (self) {
        self.keywords = keywords;
        self.city = city;
        self.jobArr = job;
        self.date = date;
    }
    return self;
}

-(NSMutableArray *)itemArray{
    if (!_itemArray) {
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"工友求职信息"];
    [self dealWithParams];
    [self.tableView setTableFooterView:[UIView new]];
    [self.tableView addHeaderWithTarget:self action:@selector(headerRefresh)];
    [self.tableView addFooterWithTarget:self action:@selector(footerRefresh)];
    [self.tableView headerBeginRefreshing];
}

-(void)dealWithParams{
    if (!self.hideHeader) {
        //添加table头
        UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, SCREENW, 44)];
        searchBar.placeholder = @"搜索";
        UIButton *button = [[UIButton alloc] initWithFrame:searchBar.frame];
        [button addTarget:self action:@selector(searchBarBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [searchBar addSubview:button];
        self.tableView.tableHeaderView = searchBar;
        
        
        if (ISLOGIN) {
            NSInteger group_id = [UserDefaultsHelper integerForKey:KEY_USER_GROUP_ID];
            if(!(group_id == 3||group_id == 10||group_id==11||group_id==12||group_id==13)){
                //显示右上角
                self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发布求职信息" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonItemClick)];
                self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
                
            }
        }
    }
    
    _job = [[NSMutableString alloc]init];
    for (int i = 0; i<_jobArr.count; i++) {
        [_job appendString:_jobArr[i]];
        if(i!=_jobArr.count-1){
            [_job appendString:@","];
        }
    }
    _end = [TimeUtils getFormatCurrentDate];
    NSDate * currentDate = [NSDate date];
    NSDate *preDate;
    switch ([_date date_id]) {
        case ALL_DAY:
            _start = @"";
            _end = @"";
            break;
        case ONE_DAY:
            preDate = [NSDate dateWithTimeInterval:-(24*60*60) sinceDate:currentDate];
            break;
        case TWO_DAY:
            preDate = [NSDate dateWithTimeInterval:-(24*60*60*2) sinceDate:currentDate];
            break;
        case THREE_DAY:
            preDate = [NSDate dateWithTimeInterval:-(24*60*60*3) sinceDate:currentDate];
            break;
        case FORE_DAY:
            preDate = [NSDate dateWithTimeInterval:-(24*60*60*4) sinceDate:currentDate];
            break;
        case ONE_WEEK:
            preDate = [NSDate dateWithTimeInterval:-(24*60*60*7) sinceDate:currentDate];;
            break;
        case TWO_WEEK:
            preDate = [NSDate dateWithTimeInterval:-(24*60*60*14) sinceDate:currentDate];
            break;
        case ONE_MONTH:
            preDate = [NSDate dateWithTimeInterval:-(24*60*60*30) sinceDate:currentDate];
            break;
        case TWO_MONTH:
            preDate = [NSDate dateWithTimeInterval:-(24*60*60*60) sinceDate:currentDate];
            break;
        default:
            break;
    }
    _start = [TimeUtils getFormatCurrentDate:preDate];
}

-(void)rightBarButtonItemClick{
    if (!ISLOGIN) {
        LWLoginCtrl *ctrl = [[LWLoginCtrl alloc] init];
        [self presentViewController:[[CustomNavigationCtrl alloc] initWithRootViewController:ctrl] animated:YES completion:nil];
        return;
    }
    ResumeCenterCtrl *ctrl =  [[ResumeCenterCtrl alloc]init];
    [self.navigationController pushViewController:ctrl animated:YES];
}

-(void)searchBarBtnClick{
    NSLog(@"searchBarBtnClick");
    RecruitmentCtrl *ctrl = [[RecruitmentCtrl alloc]initWithType:RecruitmentTypeApplyInfoSearch];
    [self.navigationController pushViewController:ctrl animated:YES];
}

-(void)headerRefresh{
    [self requestDataWithMaxId:0];
}

-(void)footerRefresh{
    NSInteger maxId = 0;
    if (self.itemArray.count) {
        ResumeRes *item = [self.itemArray lastObject];
        maxId = item.id-1;
    }
    [self requestDataWithMaxId:maxId];
}

-(void)requestDataWithMaxId:(NSInteger)maxId{
    if(ISBLANK(_keywords)){
        _keywords = @"";
    }
    if(ISBLANK(_city) || [_city isEqualToString:@"全国"]){
        _city = @"";
    }
    if(ISBLANK(_job)){
        _job = [NSMutableString string];
    }
    if(ISBLANK(_start)){
        _start = @"";
    }
    if(ISBLANK(_end)){
        _end = @"";
    }
    [HomeDataTool getResumeListWithKeyword:_keywords area:_city job:_job start:_start end:_end maxId:maxId success:^(ResumeListRes *result) {
        [Utils refreshComplete:_tableView];
        if (result.isSucc) {
            if (maxId==0) {
                [self.itemArray removeAllObjects];
            }
            [self.itemArray addObjectsFromArray:result.datas];
            [self.tableView reloadData];
        }else{
            [DialogUtils showTipsWithView:nil String:result.msg];
        }
    } failure:^(NSError *error) {
        [Utils refreshComplete:_tableView];
        [DialogUtils showTipsWithView:nil String:@"加载失败"];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ResumeItemCell *cell = [ResumeItemCell itemWithTableView:tableView];
    [cell setItemInfo:self.itemArray[indexPath.row]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ResumeCenterCtrl *ctrl = [[ResumeCenterCtrl alloc] initWithResumeRes:self.itemArray[indexPath.row]];
    [self.navigationController pushViewController:ctrl animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 65;
}

@end
